﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xpus.YouYTube.Data;
using Xpus.YouYTube.Utility;
using Xpus.YouYTube.WebSite.Models;

namespace Xpus.YouYTube.WebSite.Controllers
{
	public class VideoController : Controller
	{
		public void View(int id)
		{
			using (TDataContext db = new TDataContext())
			{
				// 获取对应的视频
				TVideo video = db.TVideos.FirstOrDefault(v => v.Id == id && v.IsDeleted == false);
				ViewData["Video"] = video;
			}

			RenderView("ViewVideo");
		}

		public void List(TVideoListType by, int pageIndex, int pageSize)
		{
			int totalVideos = 0;
			int totalPages = 0;
			TVideo[] videos = null;

			using (TDataContext db = new TDataContext())
			{
				totalVideos = db.TVideos.Select(v => v.Id).Count();
				totalPages = (totalVideos + pageSize - 1) / pageSize;

				if (by == TVideoListType.ByTime)
				{
					videos = db.TVideos.OrderByDescending(v => v.AddedTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
				}
			}

			ViewData["Videos"] = videos;
			ViewData["ListType"] = by.GetFriendlyName();
			ViewData["ListBy"] = by.ToString();
			ViewData["PageIndex"] = pageIndex;
			ViewData["PageSize"] = pageSize;
			ViewData["TotalPages"] = totalPages;

			RenderView("VideoList");
		}

		public void Recommend()
		{
			RenderView("RecommendVideo");
		}

		public void RecommendSubmit(
			string txtUserName, string txtUserUrl,
			string txtTitle, string txtDescription, string txtSource, string txtEmbeddedCode,
			string recaptcha_challenge_field, string recaptcha_response_field)
		{
			if (TIpUtility.IsIpBlocked(Request.UserHostAddress, true))
			{
				Response.Write("F对不起，您的IP地址已被屏蔽！您不能推荐视频。");
				return;
			}

			if (txtUserName == null || ((txtUserName = txtUserName.Trim()) == ""))
			{
				Response.Write("F请留下您的大名呗？");
				return;
			}

			if (txtTitle == null || ((txtTitle = txtTitle.Trim()) == ""))
			{
				Response.Write("F这么精彩的视频，不写个标题么？");
				return;
			}

			if (txtDescription == null || ((txtDescription = txtDescription.Trim()) == ""))
			{
				Response.Write("F这么精彩的视频，不简单介绍两句么？");
				return;
			}

			if (txtSource == null || ((txtSource = txtSource.Trim()) == ""))
			{
				Response.Write("F人家添加视频也不容易，把人家的链接地址填进来吧！");
				return;
			}

			if (txtEmbeddedCode == null || ((txtEmbeddedCode = txtEmbeddedCode.Trim()) == ""))
			{
				Response.Write("F您不填写嵌入代码，视频怎么能播放呢？");
				return;
			}

			if (!TReCaptcha.Verify(recaptcha_challenge_field, recaptcha_response_field))
			{
				Response.Write("F对不起，验证码错误！");
				return;
			}

			try
			{
				using (TDataContext db = new TDataContext())
				{
					TUserRecommend recommend = new TUserRecommend()
					{
						Title = txtTitle,
						Description = txtDescription,
						Source = txtSource,
						EmbeddedCode = txtEmbeddedCode,
						UserName = txtUserName,
						UserUrl = txtUserUrl == null ? "" : txtUserUrl.Trim(),
						AddedTime = DateTime.Now,
						UserIp = Request.UserHostAddress,
					};

					db.TUserRecommends.InsertOnSubmit(recommend);
					db.SubmitChanges();
				}

				Response.Write("T提交成功。谢谢您的推荐！");
			}
			catch
			{
				Response.Write("F抱歉，发生了未知错误！");
			}
		}
	}
}
